<?php

namespace App\Services\JsTan;
use Log;

class Xinshida
{
    // 订单发货
    public static function orderDelivery($params, &$msg='')
    {
        $api_url = 'http://api.eyomart.com/Grabbed/Push/waybill.html';
        $appKey = '72181c49d591ed48ac368a4ca624ab9a';

        // 处理物流公司
        $lc_id = $lc_name = '';
        // if ($params['lc_id']=='货易达__13')
        if (strpos($params['lc_id'], '货易达')!==false)
        {
            $lc_id = 'heute';
            $lc_name = '九聘';
        }
        else
        {
            $shipping_code = self::LogisticsCompany($params['logistics_company']);
            if ($shipping_code)
            {
                $lc_id = $shipping_code;
                $lc_name = $params['logistics_company'];
            }
        }

        $post_data['appid'] = 972;
        $post_data['order_sn'] = $params['so_id'] ;
        $post_data['waybill_number'] = $params['l_id'];
        $post_data['shipping_code'] = $lc_id;
        $post_data['shipping_name'] = $lc_name;
        $post_data['sign'] = self::encrypt($post_data, $appKey);

        // $post_data = [
        //     "appid" => 972,
        //     "order_sn" => "br250527157140790243",
        //     "waybill_number" => "DEUHYD500595103536EU",
        //     "shipping_code" => "C_001",
        //     "shipping_name" => null,
        //     "sign" => "1b46245ddf73e2274bd0a7b92444535d"
        // ];
        // dd($api_url, $post_data);
        Log::info('xsd_order_delivery_post_data:'. json_encode($post_data));
        $result = self::sendPost($api_url, $post_data);
        Log::info('xsd_order_delivery_post_result:'. json_encode($result));
        // dd($post_data, $result);

        if ($result['code'] == '0000')
        {
            return true;
        }
        else
        {
            $msg = $result['message'];
            return false;
        }
    }

    public static function sendPost($url, $data)
    {
        // dd($url, $data);
        $headers = [
            "Content-Type: Content-Type: application/json; charset=utf-8",
        ];
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url);
        // curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $res = curl_exec($ch);
        curl_close($ch);
        $res = json_decode($res, 1);
        // dd($res);
        return $res;
    }

    /**
     *  post提交数据
     * @param  string $url 请求Url
     * @param  array $datas 提交的数据
     * @return url响应返回的html
     */
    public static function sendPost_bak($url, $datas)
    {
        $temps = array();
        foreach ($datas as $key => $value) {
            $temps[] = sprintf('%s=%s', $key, $value);
        }

        $post_data = implode('&', $temps);
        $url_info = parse_url($url);

        if(empty($url_info['port']))
        {
            $url_info['port']=80;
        }

        $httpheader = "POST " . $url_info['path'] . " HTTP/1.0\r\n";
        $httpheader.= "Host:" . $url_info['host'] . "\r\n";
        $httpheader.= "Content-Type:application/x-www-form-urlencoded\r\n";
        $httpheader.= "Content-Length:" . strlen($post_data) . "\r\n";
        $httpheader.= "Connection:close\r\n\r\n";
        $httpheader.= $post_data;
        $fd = fsockopen($url_info['host'], $url_info['port']);
        fwrite($fd, $httpheader);
        $gets = "";
        $headerFlag = true;
        while (!feof($fd))
        {
            if (($header = @fgets($fd)) && ($header == "\r\n" || $header == "\n"))
            {
                break;
            }
        }

        while (!feof($fd))
        {
            $gets.= fread($fd, 128);
        }

        fclose($fd);

        return $gets;
    }

    /**
     * 电商Sign签名生成
     * @param data 内容
     * @param appkey Appkey
     * @return DataSign签名
     */
    public static function encrypt($params, $appkey='72181c49d591ed48ac368a4ca624ab9a')
    {
        unset($params['sign']);

        ksort($params);

        $sign = md5(json_encode($params) . $appkey);
        // dd(json_encode($params) . $appkey, $sign);
        return $sign;
    }

    /**
     * 电商Sign签名生成
     * @param data 内容
     * @param appkey Appkey
     * @return DataSign签名
     */
    public static function checkSign($params, $appkey='72181c49d591ed48ac368a4ca624ab9a')
    {
        $sign_ = $params['sign'];

        unset($params['sign']);

        ksort($params);

        $sign = md5(json_encode($params) . $appkey);

        if ($sign_ == $sign)
        {
            return true;
        }

        return false;
    }

    // 物流公司
    public static function LogisticsCompany($company_name)
    {
        $company = [
            'ems'=>'ems',
            'yuantong'=>'圆通快递',
            'yunda'=>'韵达快递',
            'zhongtong'=>'中通速递',
            'shentong'=>'申通快递',
            'shunfeng'=>'顺丰快递',
            'UEQ'=>'UEQ',
            'youbang'=>'优邦',
            'yiyouguoji'=>'易邮国际',
            'lantiankuaidi'=>'蓝天快递',
            'xunwuliu'=>'迅物流',
            'sudizhongguo'=>'速递中国',
            'yingchaowuliu'=>'英超物流',
            'huitongkuaidi'=>'百世汇通',
            'ewe'=>'EWE国际快递',
            'emsguoji'=>'EMS国际',
            'postnlchina'=>'荷兰邮政中国件',
            'tiantian'=>'天天快递',
            'annengxiaobao'=>'安能小包',
            'yousukuaidi'=>'优速快递',
            'guotong'=>'国通快递',
            'FTD'=>'富腾达物流',
            'debangwuliu'=>'德邦物流',
            'cnair'=>'CNAIR国际物流',
            'zhuozhisuyun'=>'卓志速运',
            'jieanda'=>'捷安达国际速递',
            'pjkd'=>'品骏快递',
            'AUE'=>'AUE',
            'jidikd'=>'极地快递',
            'Flywayex'=>'程光快递',
            'debangkuaidi'=>'德邦快递',
            'Xlobo'=>'贝海国际快递',
            'kuteng'=>'酷藤快递',
            'ARK'=>'方舟国际速递',
            'Yzxbao'=>'邮政小包',
            'huaxiaguoji'=>'华夏国际速递',
            'fzsd'=>'方舟速递',
            'XDexpress'=>'迅达速递',
            'GXSD'=>'光线速递',
            'RLG'=>'澳洲飞跃物流',
            'AUSE'=>'澳世速递',
            'ABWULIU'=>'澳邦物流',
            'HYKUAIDI'=>'亨洋速递',
            'sxsd'=>'三象速递',
            'EAX'=>'EAX欧亚专线',
            'FYWL'=>'飞跃物流',
            'cjwl'=>'长江物流',
            'CHS'=>'中环国际快递',
            'AusLink'=>'澳联速递',
            'FastGo'=>'速派快递',
            'anshitong'=>'安世通',
            'youban'=>'邮邦快递',
            'JDkuaidi'=>'京东快递',
            'auode'=>'澳德物流',
            'zhaijisong'=>'宅急送',
            'EFS'=>'平安快递',
            'MDKD'=>'明达快递',
            'HQGJ'=>'宏桥国际快递',
            'SNWL'=>'苏宁物流',
            'COE'=>'东方快递',
            'shunxin'=>'顺心捷达',
            'heute' => '德国货易达'
        ];

        $company_ = array_reverse($company);

        if (isset($company_[$company_name]) && $company_[$company_name])
        {
            return $company_[$company_name];
        }
        else
        {
            return false;
        }
    }

}
